

//
//  UserViewController.m
//  cookBook
//
//  Created by xalo on 16/1/22.
//  Copyright © 2016年 张广斌. All rights reserved.
//

#import "UserViewController.h"
#import "RecordHelper.h"
#import "Model.h"
#import "CollectedViewController.h"
#import "CollectionHelper.h"

@interface UserViewController ()
@property (nonatomic,strong) NSDictionary *dict;
@property (nonatomic,strong) NSArray *array;
@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"更多";
    self.dict = @{@"收藏与浏览记录":@[@"精选收藏",@"浏览记录"]};
    self.array = @[@"收藏与浏览记录"];
    

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//列数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.array.count;
}

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [self.dict[self.array[section]] count];
}

//加载单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell1";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    
    NSString *string = self.dict[self.array[indexPath.section]][indexPath.row];
    
    cell.textLabel.text = string;
    
    return cell;
}

//区头
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.array[section];
}

//进入详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //判断进入详情页面
    if (indexPath.section == 0 && indexPath.row == 1) {
        
        
        CollectedViewController *collVC = [[CollectedViewController alloc] init];
        
        //标记进入浏览记录
        collVC.number = 1;
        
        //数据库中数组
        NSArray *sqliteArray = [[RecordHelper sharedManager] selectAll];
        
        //数组反向排序
        collVC.collectionArray = [self arrayWithArray:sqliteArray];
        
        //title
        collVC.title = @"浏览记录";
        
        [self.navigationController pushViewController:collVC animated:YES];
    }
    
    //进入收藏页面
    else if (indexPath.section == 0 && indexPath.row == 0)
    {
        CollectedViewController *collVC = [[CollectedViewController alloc] init];
        
        //标记进入收藏
        collVC.number = 0;
        
        //数据库中数组
        NSArray *sqliteArray = [[CollectionHelper sharedManager] selectAll];
        
        //数组反向排序
        collVC.collectionArray = [self arrayWithArray:sqliteArray];
        
        //title
        collVC.title = @"收藏页面";
        
        [self.navigationController pushViewController:collVC animated:YES];
    }
    
}


//数组反序
- (NSMutableArray *)arrayWithArray:(NSArray *)array
{
    NSMutableArray *array1 = [NSMutableArray array];
    for (NSInteger i = 0; i <array.count; i++) {
        id object = array[array.count-i-1];
        [array1 addObject:object];
    }

    return array1;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
